package com.hoyue.interact.controller;

import com.hoyue.classroom.bean.Course;
import com.hoyue.classroom.service.CourseService;
import com.hoyue.interact.bean.Comment;
import com.hoyue.interact.bean.Matter;
import com.hoyue.interact.bean.Reply;
import com.hoyue.interact.bean.UserMatter;
import com.hoyue.interact.service.CommentService;
import com.hoyue.interact.service.MatterService;
import com.hoyue.interact.service.ReplyService;
import com.hoyue.system.bean.User;
import com.hoyue.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/matter")
public class MatterController {


    private String prefix = "interact";



    @Autowired
    MatterService matterService;
    @Autowired
    UserService userService;
    @Autowired
    CourseService courseService;
    @Autowired
    CommentService commentService;
    @Autowired
    ReplyService replyService;


    /**
     * 遍历输出话题
     * @param modelMap
     * @return
     */
    @GetMapping("/Amatter/{id}")
    public String listMatter(ModelMap modelMap, @PathVariable(value = "id")Integer id) {
        // 查询当前登录的用户信息
        Course courses = courseService.getCourse(id);
        User user = userService.getLoginUser();
        // 查询此用户创建的话题
        List<Matter> matters = matterService.ListMatter(id);
        modelMap.put("matters", matters);
        modelMap.put("username", user.getUserName());
        modelMap.put("course",courses);
        return prefix + "/interact";
    }


    /**
     * 创建话题
     * @param headline
     * @param mdetail
     * @param starttime
     * @param endtime
     * @return
     */
    @ResponseBody
  // @RequiresRoles("teacher")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public int createMatter(String headline, String mdetail, String starttime, String endtime,Integer courseid,String username) {
        // 查询当前登录的用户信息
        User user = userService.getLoginUser();
        // 为当前课程创建话题
        int matterId =matterService.insertMatter(headline,mdetail,user.getId(),user.getUserName(),endtime,starttime,courseid);
        // 将当前课程与话题绑定
        UserMatter userMatter = new UserMatter(user.getId(),matterId,courseid);//新加的一个id
        return matterService.addMatter(userMatter);
    }
    /**
     * 删除话题
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String delMatter(Integer id) {
        String msg = "1";
        try {
            matterService.delMatter(id);
            msg = "0";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * 编辑课程
     * @return
     */
    // @ResponseBody
    @GetMapping("/edit/{id}")
    public String editMatter(Model model, @PathVariable(value = "id") Integer id) {
        Matter matter = matterService.getMatter(id);
        Map<String, String> map = matterService.getMatterStr(matter);
        for (Map.Entry<String, String> item : map.entrySet()) {
            model.addAttribute(item.getKey(), item.getValue());
        }
        model.addAttribute("id", matter.getId());
        return prefix + "/edit";
    }

    /**
     * 更新话题
     *
     * @param headline
     * @param mdetail
     * @param starttime
     * @param endtime
     * @return
     */
    @ResponseBody
    // @RequiresRoles("teacher")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String updateMatter(String headline, String mdetail, String starttime, String endtime, Integer id) {
        String msg = "1";
        try {
            matterService.updateMatter(headline, mdetail, starttime, endtime, id);
            msg = "0";
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(id);
        return msg;

    }

    /**
     * 关键字查询（简单）
     */
    // @ResponseBody
    @RequestMapping(value = "/Mfind/{id}", method = RequestMethod.POST)
    public String Mfind(String mdetail, ModelMap modelMap, @PathVariable(value = "id") Integer id) {
        List<Matter> Mfind = matterService.Mfind(mdetail);
        modelMap.put("matters", Mfind);
        User user = userService.getLoginUser();
        modelMap.put("username", user.getUserName());
        Course courses = courseService.getCourse(id);
        modelMap.put("course",courses);
   //     return "rediret:/interact/interact";

        return prefix + "/interact";
    }


    /**
     * 进入单条评论
     * @param id
     * @return
     */
    @PostMapping("/topcie")
    public String cIndex(Integer id, ModelMap modelMap, HttpSession session) {
        //课程id

        modelMap.put("id",id);
        System.out.println(id);
       System.out.println(session.getAttribute("commentid"));
        Integer commentid=(Integer)session.getAttribute("commentid");
        //输出回复信息
      //  System.out.println("评论id"+commentid);
        modelMap.put("commentid",commentid);
        List<Reply> reply=replyService.ListReply(commentid);
        modelMap.put("reply",reply);
        System.out.println(reply);
        //输出特定话题信息
        List<Matter> matter=matterService.Amatter(id);
        modelMap.put("matter",matter);
        //遍历输出评论信息
        List<Comment> comment = commentService.listComment(id);
        modelMap.put("comment",comment);
        //在页面上显示当前用户名
        User user = userService.getLoginUser();
        modelMap.put("username", user.getUserName());
        //在页面上显示回复评论
        modelMap.put("course.id",id);
        return prefix + "/topicDiscuss";
    }
    /**
     * 回复的信息展示
     */
    public String reply()
    {
        return null;
    }

    /**
     * 置顶功能（未完成）
     */
    @ResponseBody
    @PostMapping("/istop")
    public String Istop(ModelMap modelMap, Integer id, Integer istop) {

        System.out.println(id);
        System.out.println(istop+1);
        matterService.Istopupdate(istop);
        User user = userService.getLoginUser();
        // 查询此用户创建的话题
        List<Matter> matters = matterService.ListMatter(user.getId());
        modelMap.put("matters", matters);
        modelMap.put("username", user.getUserName());
        return prefix + "/interact";
    }



}


